/* 
 * @(#) ChangeAction.java  1.0  2004-4-22 
 * 
 * Copyright 2003 by LongTop Corporation. 
 * All rights reserved. 
 * 
 * This software is the confidential and proprietary information of 
 * LongTop Corporation ("Confidential Information").  You 
 * shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement 
 * you entered into with LongTop. 
 */

package com.cnc.fd.web;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
 

import com.cnc.bas.db.JdbcDao;
import com.cnc.bas.query.PageInfo;
import com.cnc.bas.util.LTUtil;
import com.cnc.bas.web.BasMapForm;
import com.cnc.bas.web.SecureBaseDispatchAction;
 
import com.cnc.fd.hiber.UserUploadserver;
import com.cnc.fd.service.FdDao;
import com.cnc.fd.service.IFdService;
import com.cnc.fd.util.FdProperties;

/**
 * @author LW
 * @version 1.0 
 */
public class UserServerAction extends SecureBaseDispatchAction {

    private FdDao fdDao = null;
    private IFdService service = null;
    private JdbcDao jdbcdao = (JdbcDao)this.getBean(JdbcDao.BEAN_ID);
    private static final int defaultPageSize = 20;
    private static final int maxPageSize = 2000;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        fdDao = (FdDao) this.getBean(FdDao.BEAN_ID);
        service = (IFdService)this.getBean(IFdService.BEAN_ID);
    }
    
    public ActionForward userServerQuery(ActionMapping mapping,ActionForm form,HttpServletRequest request,
           HttpServletResponse response) throws Exception {       
        return mapping.findForward("search");
    }
 
    
    public ActionForward userServerList(ActionMapping mapping, ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {	
	HashMap hs = ((BasMapForm) form).getHs();
	int intCurrentPage = 1;
	String currPage = request.getParameter("intCurrentPage");
	try{
		intCurrentPage = Integer.parseInt(currPage);
	}
	catch(Exception e){
	}
	
	int intTotalPages = 1;
	String totalPage = request.getParameter("intTotalPages");
	try{
		intTotalPages = Integer.parseInt(totalPage);
	}
	catch(Exception e){
		
	}
	int pageSize = (intTotalPages==-1)?maxPageSize:defaultPageSize;
	PageInfo page = new PageInfo(intCurrentPage,pageSize);
	fdDao.getUserServer(hs,page);
	LTUtil.setPageData(request,page);	
	return mapping.findForward("list");
    }
    
//    public void initUserServer(HttpServletRequest request){
//	String sql = "select customerId,serverId from FD_Customer_Uploadserver";
//	List list = jdbcdao.queryForList(sql);
//	String params = "";
//	for(int i = 0;i<list.size();i++){
//	    Map map = (Map)list.get(i);
//	    String group = map.get("customerId")+":" +map.get("serverId");	
//	    params+=(params==""?group:(","+group));
//	}	
//	request.setAttribute("params", params);
//    }
    
    public boolean checkExist(UserUploadserver userServer){
	String sql = "select * from FD_Customer_Uploadserver where customerId = ? and serverId = ?";
	List<String> params = new ArrayList<String>();
	params.add(userServer.getCustomerid());
	params.add(userServer.getServerid());
	List list = jdbcdao.queryForList(sql,params.toArray());
	if(null==list||list.isEmpty()) return true;
	return false;
    }
    
    
    
    public ActionForward create(ActionMapping mapping, ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {
//	initUserServer(request);
	return mapping.findForward("edit");
    }

    
    public ActionForward edit(ActionMapping mapping, ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {	
	String id = request.getParameter("id");	 
	 
//	initUserServer(request);
	return mapping.findForward("edit");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {	
	UserUploadserver userServer  = ((UserServerForm) form).getUserServer();	
	if(!checkExist(userServer)) throw new Exception("您已经为当前客户添加了该服务器，务须重新添加！");
	String uploadType = request.getParameter("uploadType");
	String bakServer = request.getParameter("bakServer");
	String userAccount = userServer.getCustomerid();
	if(uploadType.equals("ftp")){
	    {//主机		
		sshUploadServer(userServer.getServerid(),userAccount);		
	    }
	    {//备机
		if(!(null==bakServer||"".equals(bakServer))) sshUploadServer(bakServer,userAccount);	
	    }
	}
	 
	return mapping.findForward("edit");
    }
    
    public void delete(ActionMapping mapping, ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {	
	String id = request.getParameter("id");		
	 
	
    }
    
    class FtpAccount implements Runnable{
	private String host;
	private int port;
	private String user;
	private String password;
	private String command;
	public FtpAccount(String host,int port,String user,String password,String command){
	   this.host = host;
	   this.port = port;
	   this.user = user;
	   this.password = password;
	   this.command = command;
	   Thread t = new Thread(this);
	   t.setDaemon(true);
	   t.start();
	}
	public void run(){
	    
	}
    }

    public void sshUploadServer(String serverIpOrserverId,String userAccount) throws Exception{
	Map loginInfo = fdDao.getSshLoginInfo(serverIpOrserverId);
	if(null==loginInfo||loginInfo.isEmpty()) throw new Exception("获取远程连接信息失败!"); 
	String host = (String)loginInfo.get("host");
	int port = Integer.parseInt(String.valueOf(loginInfo.get("port"))) ;
	String user = "root";
	String password = (String)loginInfo.get("password");
	String command = FdProperties.getProperty("makeusershell")+" "+userAccount;
	this.new FtpAccount(host,port,user,password,command);
    }
    
  
}

